// 第10节：查询：find的不等修饰符

/* 
    MongoDB的查找操作我们会分几节课来讲，因为内容还是比较多的，而且在开发中查找是应用最多的操作，几乎每个模块都会用到，
  所以查找的部分将是本套课的重中之重。这节课我们先来看看简单的查询条件，也了解一下find基础用法。如果你以前操作过关系
  型数据库，比如MySql，你会对>大于)，<(小于),=(等于)这些东西很熟悉，但是非关系型数据库不能直接使用这些符号，稍有区别。
*/

// 构造数据：
  /* 
    我们需要构造更多的数据到集合中，这样我们才能很好的讲解查询条件，下面代码你可以直接复制进行添加。当然你也可以自己随
    意加一些数据到集合中，只要方便我们学习就可以了。
  */

var workmate1 = {
  name: 'JSPang',
  age: 33,
  sex: 1,
  job: '前端',
  skill: {
    skillOne: 'HTML+CSS',
    skillTwo: 'JavaScript',
    skillThree: 'PHP'
  },
  regeditTime: new Date(),
  interest: []
}
var workmate2 = {
  name: 'ShengLei',
  age: 31,
  sex: 1,
  job: 'JAVA后端',
  skill: {
    skillOne: 'HTML+CSS',
    skillTwo: 'J2EE',
    skillThree: 'PPT'
  },
  regeditTime: new Date(),
  interest: []
}
var workmate3 = {
  name: 'MinJie',
  age: 18,
  sex: 0,
  job: 'UI',
  skill: {
    skillOne: 'PhotoShop',
    skillTwo: 'UI',
    skillThree: 'PPT'
  },
  regeditTime: new Date(),
  interest: []
}
var workmate4 = {
  name: 'XiaoWang',
  age: 25,
  sex: 1,
  job: 'UI',
  skill: {
    skillOne: 'PhotoShop',
    skillTwo: 'UI',
    skillThree: 'PPT'
  },
  regeditTime: new Date(),
  interest: []
}
var workmate5 = {
  name: 'LiangPeng',
  age: 28,
  sex: 1,
  job: '前端',
  skill: {
    skillOne: 'HTML+CSS',
    skillTwo: 'JavaScript',
  },
  regeditTime: new Date(),
  interest: []
}
var workmate6 = {
  name: 'HouFei',
  age: 25,
  sex: 0,
  job: '前端',
  skill: {
    skillOne: 'HTML+CSS',
    skillTwo: 'JavaScript',
  },
  regeditTime: new Date(),
  interest: []
}
var workmate7 = {
  name: 'LiuYan',
  age: 35,
  sex: 0,
  job: '美工',
  skill: {
    skillOne: 'PhotoShop',
    skillTwo: 'CAD',
  },
  regeditTime: new Date(),
  interest: []
}
var workmate8 = {
  name: 'DingLu',
  age: 20,
  sex: 0,
  job: '美工',
  skill: {
    skillOne: 'PhotoShop',
    skillTwo: 'CAD',
  },
  regeditTime: new Date(),
  interest: []
}
var workmate9 = {
  name: 'JiaPeng',
  age: 29,
  sex: 1,
  job: '前端',
  skill: {
    skillOne: 'HTML+CSS',
    skillTwo: 'JavaScript',
    skillThree: 'PHP'
  },
  regeditTime: new Date(),
  interest: []
}
var workmate10 = {
  name: 'LiJia',
  age: 26,
  sex: 0,
  job: '前端',
  skill: {
    skillOne: 'HTML+CSS',
    skillTwo: 'JavaScript',
    skillThree: 'PHP'
  },
  regeditTime: new Date(),
  interest: []
}
var db = connect('demo10');
var workmateArray = [workmate1, workmate2, workmate3, workmate4, workmate5, workmate6, workmate7, workmate8, workmate9, workmate10];
db.workmate.insert(workmateArray);
print('[SUCCESS]：The data was inserted successfully');

// 简单查找：
  // 比如我们现在要查找数据中技能一会HTML+CSS的所有人。那我们直接进行查找加条件就可以了。
  // db.workmate.find({"skill.skillOne":"HTML+CSS"})
  // 这时候我们不能使用load来载入了，以后我会给大家讲使用的方法，这里先用比较笨的方法，使用粘贴复制的方法执行。
  /* 
    结果:
    { "_id" : ObjectId("5b8e908d081ab44f912d4485"), "name" : "JSPang", "age" : 33, "sex" : 1, "job" : "前端", "skill" : { "skillOne" : "HTML+CSS", "skillTwo" : "JavaScript", "skillThree" : "PHP" }, "regeditTime" : ISODate("2018-09-04T14:02:53.401Z"), "interest" : [ ] }
    { "_id" : ObjectId("5b8e908d081ab44f912d4486"), "name" : "ShengLei", "age" : 31, "sex" : 1, "job" : "JAVA后端", "skill" : { "skillOne" : "HTML+CSS", "skillTwo" : "J2EE", "skillThree" : "PPT" }, "regeditTime" : ISODate("2018-09-04T14:02:53.401Z"), "interest" : [ ] }
    { "_id" : ObjectId("5b8e908d081ab44f912d4489"), "name" : "LiangPeng", "age" : 28, "sex" : 1, "job" : "前端", "skill" : { "skillOne" : "HTML+CSS", "skillTwo" : "JavaScript" }, "regeditTime" : ISODate("2018-09-04T14:02:53.401Z"), "interest" : [ ] }
    { "_id" : ObjectId("5b8e908d081ab44f912d448a"), "name" : "HouFei", "age" : 25, "sex" : 0, "job" : "前端", "skill" : { "skillOne" : "HTML+CSS", "skillTwo" : "JavaScript" }, "regeditTime" : ISODate("2018-09-04T14:02:53.401Z"), "interest" : [ ] }
    { "_id" : ObjectId("5b8e908d081ab44f912d448d"), "name" : "JiaPeng", "age" : 29, "sex" : 1, "job" : "前端", "skill" : { "skillOne" : "HTML+CSS", "skillTwo" : "JavaScript", "skillThree" : "PHP" }, "regeditTime" : ISODate("2018-09-04T14:02:53.401Z"), "interest" : [ ] }
    { "_id" : ObjectId("5b8e908d081ab44f912d448e"), "name" : "LiJia", "age" : 26, "sex" : 0, "job" : "前端", "skill" : { "skillOne" : "HTML+CSS", "skillTwo"
    : "JavaScript", "skillThree" : "PHP" }, "regeditTime" : ISODate("2018-09-04T14:02:53.401Z"), "interest" : [ ] }
  */

// 筛选字段
  /* 
    现在返回来的数据项太多，太乱，有时候我们的程序并不需要这么多选项。比如我们只需要姓名和技能就可以了。这时候我们需要写
    第二个参数，看下面的代码。
  */
 /* 
  db.workmate.find(
    {"skill.skillOne":"HTML+CSS"},
    {name:true,"skill.skillOne":true}
  )
 */
  /* 
    结果:
    { "_id" : ObjectId("5b8e908d081ab44f912d4485"), "name" : "JSPang", "skill" : { "skillOne" : "HTML+CSS" } }
    { "_id" : ObjectId("5b8e908d081ab44f912d4486"), "name" : "ShengLei", "skill" : { "skillOne" : "HTML+CSS" } }
    { "_id" : ObjectId("5b8e908d081ab44f912d4489"), "name" : "LiangPeng", "skill" : { "skillOne" : "HTML+CSS" } }
    { "_id" : ObjectId("5b8e908d081ab44f912d448a"), "name" : "HouFei", "skill" : { "skillOne" : "HTML+CSS" } }
    { "_id" : ObjectId("5b8e908d081ab44f912d448d"), "name" : "JiaPeng", "skill" : { "skillOne" : "HTML+CSS" } }
    { "_id" : ObjectId("5b8e908d081ab44f912d448e"), "name" : "LiJia", "skill" : { "skillOne" : "HTML+CSS" } }

  */

  // 细心的小伙伴会发现还不够完美，多了一个ID字段，这个也不是我们想要的，这时候只要把_id:false就可以了。当然这里的false
  // 和true，也可以用0和1表示。

  /* 
    代码:
    db.workmate.find(
      {'skill.skillOne':'HTML+CSS'},
      {
        name:true,
        'skill.skillOne':true,
        '_id':0
      }
    )
  */

  /* 
    结果:
    { "name" : "JSPang", "skill" : { "skillOne" : "HTML+CSS" } }
    { "name" : "ShengLei", "skill" : { "skillOne" : "HTML+CSS" } }
    { "name" : "LiangPeng", "skill" : { "skillOne" : "HTML+CSS" } }
    { "name" : "HouFei", "skill" : { "skillOne" : "HTML+CSS" } }
    { "name" : "JiaPeng", "skill" : { "skillOne" : "HTML+CSS" } }
    { "name" : "LiJia", "skill" : { "skillOne" : "HTML+CSS" } }
  */

  // 其实这些查找操作，都是在作等于的阶段，但是不光只有等于查询，我们需要更多的查询条件。


// 不等修饰符
  /* 
    小于($lt):英文全称less-than
    小于等于($lte)：英文全称less-than-equal
    大于($gt):英文全称greater-than
    大于等于($gte):英文全称greater-than-equal
    不等于($ne):英文全称not-equal 
  */
  
  // 我们现在要查找一下，公司内年龄小于30大于25岁的人员(显示姓名,爱好1,年龄)。看下面的代码。
  /* 
    db.workmate.find(
      {
        age:{$lt:30,$gt:25}
      },
      {
        name:true,
        'skill.skillOne':true,
        '_id':false,
        'age':true
      }
    )
  */

  /* 
    结果
    { "name" : "LiangPeng", "age" : 28, "skill" : { "skillOne" : "HTML+CSS" } }
    { "name" : "JiaPeng", "age" : 29, "skill" : { "skillOne" : "HTML+CSS" } }
    { "name" : "LiJia", "age" : 26, "skill" : { "skillOne" : "HTML+CSS" } }
  */


// 日期查找
  //MongoDB也提供了方便的日期查找方法，现在我们要查找注册日期大于2018年1月10日的数据，我们可以这样写代码。
  /* 
    代码:
    db.workmate.find(
      {
        regeditTime:{$gt:startDate}
      },
      {
        name:true,
        age:true,
        'skill,skillOne':true,
        '_id':0
      }
    )
  */

  /* 
    结果: 10个就对了
    { "name" : "JSPang", "age" : 33 }
    { "name" : "ShengLei", "age" : 31 }
    { "name" : "MinJie", "age" : 18 }
    { "name" : "XiaoWang", "age" : 25 }
    { "name" : "LiangPeng", "age" : 28 }
    { "name" : "HouFei", "age" : 25 }
    { "name" : "LiuYan", "age" : 35 }
    { "name" : "DingLu", "age" : 20 }
    { "name" : "JiaPeng", "age" : 29 }
    { "name" : "LiJia", "age" : 26 }
  */